package com.datax.web.mail;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;


public class SendMailUtil {

	public static void sendMail(MailVo mailVo) throws Exception
	{
		String from="g-tymx.sh@chinatelecom.cn";
		String key = "Jiekou@12";
//		String from  = "603215836@qq.com";
//		String key = "pclqfjpyzwcybdie";
		Properties props=System.getProperties();
		props.put("mail.smtp.auth","true");
		props.put("mail.host", "10.141.72.4");
//		props.put("mail.host", "smtp.chinatelecom.cn");
		props.put("mail.transport.protocol", "smtp");
		Session session = Session.getInstance(props);
		Message message = new MimeMessage(session);

		message.setSubject(mailVo.getSubject());
		message.setSentDate(new Date());
		message.setContent(mailVo.getMessageContext(), "text/html;charset=UTF-8");
		message.setFrom(new InternetAddress(from,"浩鲸-数据处理团队", "UTF-8"));

		//收件人
		List<Address> list = new ArrayList<Address>();
		for (Address address : mailVo.getRecipientTo()){
			list.add(address);
			message.addRecipient(MimeMessage.RecipientType.TO, (InternetAddress) address);
		}
		for (Address address : mailVo.getRecipientCc()){
			list.add(address);
			message.addRecipient(MimeMessage.RecipientType.CC, (InternetAddress) address);
		}
		Address[] addresses = new Address[list.size()];
		list.toArray(addresses);

		Transport transport = session.getTransport();
		transport.connect("10.141.72.4", from, key);
//		transport.connect("smtp.chinatelecom.cn", from, key);
//		transport.connect("smtp.qq.com", from, key);
		transport.sendMessage(message, addresses);
		transport.close();
	}
}
